package pl.gwt.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pl.gwt.client.common.Client;
import pl.gwt.client.common.Project;

import pl.gwt.client.ClientDetailsService;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ClientDetailsServiceImpl extends RemoteServiceServlet implements ClientDetailsService {

	private static final long serialVersionUID = -2512286892692578018L;
	
	private DbConn dbconn;
	
	public ClientDetailsServiceImpl(){
		this.dbconn = new DbConn();
	}
	
	public Client getClient(String clientStr){		
		Client client = new Client();		
		String query = "select * from clients where name='" + clientStr + "'";	
		
		try {
			Connection conn = dbconn.getConn();
			Statement select = conn.createStatement();
			ResultSet result = select.executeQuery(query);		
			
			if(result.next()){
			    client.init(result.getString(2), result.getString(3), result.getString(4), result.getString(5),
			    		result.getString(6), null);	    
			}	
			select.close();
		    result.close();
		    conn.close();
		    
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return client;
	}

	public List<String> getProjects(String client) {
		// TODO Auto-generated method stub
		
		ArrayList<String> projects = new ArrayList<String>();
		projects.add("GWT CRM");
		projects.add("PokerManager");
		projects.add("Kass");
		projects.add("AGH studenci");
		
		return projects;
	}

	public String addClient(Client client) {
				
		if(client != null){//TODO: mozheno ubrat potom		
			String query = "INSERT INTO clients ([name], phone_no, address, country, city)" + //, state)" +
					" VALUES ( '" + client.getName()+ "','" + client.getPhone_no() +"','" + client.getAddress() + "','" + 
					client.getCountry() + "', '" + client.getCity() + "' )"; //, '" + client.getState() +"' )";
		
			try {
				Connection conn = dbconn.getConn();
				Statement select = conn.createStatement();
				//ResultSet result = select.executeQuery(query);
				select.execute(query);

				select.close();
			    //result.close();
			    conn.close();
			    
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		
		}
		return "";
	}

	public String setClient() {
		// TODO Auto-generated method stub
		
		return "";
	}


}
